##
#   Project: gmediacatalog - Catalog your media to find them later
#    Author: Fabio Castelli <muflone@vbsimple.net>
# Copyright: 2010 Fabio Castelli
#   License: GPL-2+
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the Free
#  Software Foundation; either version 2 of the License, or (at your option)
#  any later version.
# 
#  This program is distributed in the hope that it will be useful, but WITHOUT
#  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
#  more details.
# 
# On Debian GNU/Linux systems, the full text of the GNU General Public License
# can be found in the file /usr/share/common-licenses/GPL-2.
##

import gio

class GIODevices(object):
  def __init__(self):
    self.devices = {}
    device = gio.File('computer:///')
    for device_info in device.enumerate_children('standard::*'):
      dev_name = device_info.get_display_name()
      self.devices[dev_name] = (
        dev_name,
        device_info.get_file_type(),
        device_info.get_attribute_string('standard::target-uri')
      )

  def _find_by_uri(self, uri):
    for dev_name, dev_type, dev_uri in self.devices.itervalues():
      if dev_uri == uri:
        # URI was found
        return (dev_name, dev_type, dev_uri)
    else:
      # URI not found
      return None

  def find_name_by_uri(self, uri):
    device = self._find_by_uri(uri)
    return device and device[0] or ''

  def find_type_by_uri(self, uri):
    device = self._find_by_uri(uri)
    return device and device[1] or ''

  def find_uri_by_uri(self, uri):
    device = self._find_by_uri(uri)
    return device and device[2] or ''
